package arm.mbean;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.management.MBeanServer;
import javax.management.ObjectName;
import javax.management.remote.JMXConnectorServer;
import javax.management.remote.JMXConnectorServerFactory;
import javax.management.remote.JMXServiceURL;

import org.apache.log4j.Logger;

public class ArmConnectorServer {
	
	private static final Logger log = Logger.getLogger(ArmConnectorServer.class);
	private static final String DEFUALT_URL="service:jmx:rmi://localhost:9875";
	private String serviceURL=DEFUALT_URL;
	private JMXConnectorServer connectorServer;
	private Map environment = new HashMap();
	private boolean threaded=false;
	private boolean deamon = false;
	private ObjectName objName;
	private MBeanServer mbeanServer;
	private ArmMBeanServer armMBeanServer;
	
	
	public JMXConnectorServer createConnectorServer() throws Exception
	{
		if(mbeanServer==null)
		{
			mbeanServer = armMBeanServer.createMBeanServer();
		}
		if(serviceURL==null)
		{
			serviceURL = DEFUALT_URL;
			log.info("not found serviceURL property configuration information with default value");
		}
		JMXServiceURL serviceUrl = new JMXServiceURL(serviceURL);
		connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(serviceUrl, environment, mbeanServer);
		if(objName!=null)
		{
			mbeanServer.registerMBean(connectorServer, objName);
		}
		if(this.threaded)
		{
			Thread connectorThread = new Thread(){
				
				public void run()
				{
					try {
						connectorServer.start();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			};
			connectorThread.setName("JMX Connector Thread [" + this.serviceURL + "]");
			connectorThread.setDaemon(deamon);
			connectorThread.start();
		}
		else
		{
			connectorServer.start();
		}
		log.info("JMX connector Server started:"+this.connectorServer);
		log.info("JMX connector Server URL:"+this.serviceURL);
		return connectorServer;
	}

	public boolean isActive()
	{
		if(connectorServer!=null)
		{
			return connectorServer.isActive();
		}
		return false;
	}
	public void stop() throws IOException
	{
		if(connectorServer.isActive())
		{
			connectorServer.stop();
		}
	}

	public String getServiceURL() {
		return serviceURL;
	}


	public void setServiceURL(String serviceURL) {
		this.serviceURL = serviceURL;
	}


	public JMXConnectorServer getConnectorServer() {
		return connectorServer;
	}


	public void setConnectorServer(JMXConnectorServer connectorServer) {
		this.connectorServer = connectorServer;
	}


	public Map getEnvironment() {
		return environment;
	}


	public void setEnvironment(Map environment) {
		this.environment = environment;
	}


	public boolean isThreaded() {
		return threaded;
	}


	public void setThreaded(boolean threaded) {
		this.threaded = threaded;
	}


	public boolean isDeamon() {
		return deamon;
	}


	public void setDeamon(boolean deamon) {
		this.deamon = deamon;
	}


	public ObjectName getObjName() {
		return objName;
	}


	public void setObjName(ObjectName objName) {
		this.objName = objName;
	}


	public MBeanServer getMbeanServer() {
		return mbeanServer;
	}


	public void setMbeanServer(MBeanServer mbeanServer) {
		this.mbeanServer = mbeanServer;
	}


	public ArmMBeanServer getArmMBeanServer() {
		return armMBeanServer;
	}


	public void setArmMBeanServer(ArmMBeanServer armMBeanServer) {
		this.armMBeanServer = armMBeanServer;
	}

	
}
